﻿/**
 * 说明：简单的书籍管理系统，具有增加、查询、删除功能
 * 时间：2024-07-11
 * 作者：。。。
 */

#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;
struct Book {
    string isbn;
    unsigned int units_sold;
    double selling_price;
    double sales_price;

    void show() {
        cout << "ISBN: " << isbn << " 销量: " << units_sold << " 原价: " << selling_price << " 实际售价:" << sales_price << endl;
    }
};

/**
 * 向map中，添加书籍对象
 */
void _Add_Book(map<string, Book>& Books, Book& book) {
    if (Books.find(book.isbn) == Books.end()) {
        Books[book.isbn] = book;
    }
    else {
        Books[book.isbn].sales_price = (Books[book.isbn].sales_price * Books[book.isbn].units_sold + book.units_sold * book.sales_price) / (Books[book.isbn].units_sold + book.units_sold);
        Books[book.isbn].units_sold += book.units_sold;
    }
}

/**
 *对文件系统中的数据进行加载，放入map中
 *params: map<string, Book> Books 存放书籍对象的map, Book是书籍的结构体
 *params: string file_name 存放数据的文件名
 */
void _Init(map<string, Book>& Books, const string& file_name) {
    ifstream in(file_name, ios::in);
    istringstream istr;
    string line;
    Book book;
    while (getline(in, line)) {
        istr.clear();
        istr.str(line);
        istr >> book.isbn >> book.units_sold >> book.selling_price >> book.sales_price;
        _Add_Book(Books, book);
    }
    in.close();
}

void _Search_By_Isbn(map<string, Book>& Books, const string isbn) {
    if (Books.find(isbn) != Books.end()) {
        Books[isbn].show();
    }
    else {
        cout << "没有查找到该书籍的信息" << endl;
    }

}

void _Add_New_Book(map<string, Book>& Books) {
    cout << "请输入书籍的信息(ISBN、销量、原价、实际售价): " << endl;
    Book book;
    cin >> book.isbn >> book.units_sold >> book.selling_price >> book.sales_price;
    if (!book.isbn.empty() && book.selling_price && book.sales_price) {
        _Add_Book(Books, book);
    }
    else {
        cout << "输入数据不合法，添加书籍失败" << endl;
    }
}

void _Delete_By_Isbn(map<string, Book>& Books, const string isbn) {
    map<string, Book>::iterator it;
    it = Books.find(isbn);
    if (it != Books.end()) {
        cout << "删除书籍成功，该书籍的详细信息如下：" << endl;
        it->second.show();
        Books.erase(it);
    }
    else {
        cout << "书籍不存在，删除失败" << endl;
    }
}

/**
 * 系统退出功能函数，退出时数据保存到本地文件中
 */
void _Exit(map<string, Book>& Books, const string& file_name) {
    //退出需要将Map中的数据写入本地
    fstream out(file_name, ios::out | ios::trunc);
    for (auto& w : Books) {
        out << w.second.isbn << " " << w.second.units_sold << " " << w.second.selling_price << " " << w.second.sales_price << endl;
    }
    out.close();
}

/**
 * 展示指令函数
 */
void _Display() {
    cout <<"\
           ********************************\n\
           *     1  -  search by isbn     *\n\
           *     2  -  add new Book       *\n\
           *     3  -  delete by isbn     *\n\
           *     q  -  exit               *\n\
           ********************************" << endl;
}

int main()
{
    map<string, Book> Books;
    _Init(Books, "book_sales.txt");
    cout << " \
             >>>>>> 程序加载完成 <<<<<<<" << endl;
    _Display();
    string cmd;
    string isbn;
    while (true) {
        cout << "请输入指令:" << endl;
        cin >> cmd;
        if (cmd == "1") {
            cout << "请输入书籍的ISBN: " << endl;
            cin >> isbn;
            _Search_By_Isbn(Books, isbn);
        }
        else if (cmd == "2") {
            _Add_New_Book(Books);
        }
        else if (cmd == "3") {
            cout << "请输入书籍的ISBN: " << endl;
            cin >> isbn;
            _Delete_By_Isbn(Books, isbn);
        }
        else if (cmd == "q") {
            _Exit(Books, "book_sales.txt");
            return -1;
        }
    }
}


